package com.datoucai.service.impl;

import com.alibaba.fastjson.JSON;
import com.datoucai.dto.QueryReplyInfoDto;
import com.datoucai.entity.CommentEntity;
import com.datoucai.entity.ReplyParam;
import com.datoucai.mapper.CommentMapper;
import com.datoucai.param.ReplyInfoDto;
import com.datoucai.service.IReplyService;
import com.datoucai.utils.CommentExtendUtils;
import com.datoucai.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * 回复服务类
 *
 * @date 2024/09/04
 */
@Service
@Slf4j
public class ReplyServiceImpl implements IReplyService {
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public List<ReplyInfoDto> queryMoreReplyInfoList(QueryReplyInfoDto dto) {
        try {
            log.info("查询更多回复-service层-queryMoreReplyInfoList-入参:{}",JSON.toJSONString(dto));
            ReplyParam replyParam = new ReplyParam();
            replyParam.setCommentId(dto.getCommentId());
            replyParam.setReplyId(dto.getReplyId());
            replyParam.setModule(dto.getModule());
            replyParam.setResourceId(dto.getResourceId());
            replyParam.setIsDelete(0);
            replyParam.setLimit(dto.getReplyNum());
            replyParam.setOrderBy("create_time");
            replyParam.setOrderDirection("desc");
            List<CommentEntity> commentEntities = commentMapper.queryMoreReplyByParam(replyParam);
            List<ReplyInfoDto> replyInfoDtoList = buildReplyInfoDtoList(commentEntities);
            log.info("查询更多回复-service层-queryMoreReplyInfoList-出参:{}",JSON.toJSONString(replyInfoDtoList));
            return replyInfoDtoList;
        }catch (Exception e){
            log.error("查询更多回复-service层-queryMoreReplyInfoList-异常:{}",JSON.toJSONString(dto));
            return new ArrayList<>();
        }
    }

    private List<ReplyInfoDto> buildReplyInfoDtoList(List<CommentEntity> commentEntities) {
        if(CollectionUtils.isEmpty(commentEntities)){
            return new ArrayList<>();
        }
        List<ReplyInfoDto> replyInfoDtoList = new ArrayList<>();

        for (int i = 0; i < commentEntities.size(); i++) {
            CommentEntity commentEntity = commentEntities.get(i);
            ReplyInfoDto target = new ReplyInfoDto();
            target.setUserId(commentEntity.getUserId()+"");
            target.setId(commentEntity.getId()+"");
            target.setRootId(commentEntity.getRootCommentId()+"");
            target.setResourceId(commentEntity.getResourceId()+"");
            target.setModule(commentEntity.getModule());
            target.setUsername(CommentExtendUtils.getUsernameOnExtend(commentEntity.getExtend()));
            target.setContent(commentEntity.getContent());
            target.setStarNum(commentEntity.getStarNum());
            target.setReplyTime(DateUtils.date2Str(commentEntity.getCreateTime(),DateUtils.YYYY_MM_DD_HH_MM_SS));
            target.setAvatar(CommentExtendUtils.getAvatarOnExtend(commentEntity.getExtend()));
            target.setRepliedName(CommentExtendUtils.getRepliedUsernameOnExtend(commentEntity.getExtend()));
            target.setParentId(commentEntity.getParentCommentId()!=null?commentEntity.getParentCommentId()+"":null);
            target.setStatus(commentEntity.getStatus());
            replyInfoDtoList.add(target);
        }
        return replyInfoDtoList;
    }
}
